<?php
declare (strict_types = 1);

namespace app\controller;

use app\model\Ones;
use think\facade\Validate;
use think\Request;

class One
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Ones $ones)
    {
        $res=$ones->getAll();
        if($res){
            return json(['code'=>0,'msg'=>'展示成功','data'=>$res]);
        }else{
            return json(['code'=>1,'msg'=>'展示失败','data'=>'']);
        }
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request, Ones $ones)
    {
        $data['name']=$request->param('name');
        $data['city']=$request->param('city');
        $data['num']=$request->param('num');
        $rule =   [
        'name'  => 'require|max:30',
        'city'   => 'require',
        ];

        $message  =   [
        'name.require' => '名称必须',
        'name.max'     => '名称最多不能超过30个字符',
        'city.require' => '城市必须',
        ];
        $validate=Validate::rule($rule)->message($message);
        if(!$validate->check($data)){
            return json(['code'=>1,'msg'=>$validate->getError(),'data'=>'']);
        }
        $res=$ones->addOne($data);
        if($res){
            return json(['code'=>0,'msg'=>'添加成功','data'=>$data]);
        }else{
            return json(['code'=>1,'msg'=>'添加失败','data'=>'']);
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id,Ones $ones)
    {
        $where['id']=$id;
        $data['name']=$request->param('name');
        $data['city']=$request->param('city');
        $data['num']=$request->param('num');
        $rule =   [
            'name'  => 'require|max:30',
            'city'   => 'require',
        ];

        $message  =   [
            'name.require' => '名称必须',
            'name.max'     => '名称最多不能超过30个字符',
            'city.require' => '城市必须',
        ];
        $validate=Validate::rule($rule)->message($message);
        if(!$validate->check($data)){
            return json(['code'=>1,'msg'=>$validate->getError(),'data'=>'']);
        }
        $res=$ones->upOne($where,$data);
        if($res){
            return json(['code'=>0,'msg'=>'修改成功','data'=>$data]);
        }else{
            return json(['code'=>1,'msg'=>'修改失败','data'=>'']);
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id,Ones $ones)
    {
        $where['id']=$id;
        $res=$ones->delOne($where);
        if($res){
            return json(['code'=>0,'msg'=>'删除成功','data'=>$res]);
        }else{
            return json(['code'=>1,'msg'=>'删除失败','data'=>'']);
        }
    }
}
